<?php
	include("initialize.php");
	/**
	From this page, a teacher can generate various reports from his class records.
	Among the reports that can be generated are:
		1) Printer-friendly class record.
		   Teacher is just taken to grades.php but this time, he cannot edit grades.
		   
		   TODO: Maybe I should use POST instead of GET in setting the printerfriendly
		   flag for grades.php . When using GET, a teacher can simply delete the
		   printerfrienfly tag from the URL, reload, and be able to edit grades.
		   
		2) Class record with student marks
		   Class record but with student grades transmuted according to the grading scheme
		   set for this class.
		   
		3) Comma-separated values file
		   Allowes a teacher to download the PHP file as a CSV file which can be opened
		   in a spreadsheet application
	
	@author Chad Estioco
	@version Second Semester, AY 2010-2011
	*/
?>
<html>
<head>
	<title>GradeGrid - Generate Reports</title>
	<?php include("header.php"); ?>
	<script type="text/javascript" language="javascript">
		function generateReport(){
			var classid = document.getElementById("classes").value;
			if(document.getElementById("pfrecord").checked){
				var url = "grades.php?classid=" + classid + "&printerfriendly=t"
				window.open(url, "_blank")
			} else if(document.getElementById("csv").checked){
				var url = "csv.php?classid=" + classid
				window.open(url, "_blank")
			} else if(document.getElementById("markedrecord").checked){
				var url = "markedrecord.php?classid=" + classid
				window.open(url, "_blank")
			}
		}
	</script>
</head>
<body onLoad="writeCopyright()">
	<div class="main">
		
		<?php include("boilerplate.php"); ?>
		<?php include("loginnavigation.php"); ?>
		
		<?php if($_SESSION["logged_in"]): ?>
			<div style="float: left; max-width: 70%;">
			<div class="form_section"><h1>Generate Reports</h1></div>
			<p>Generate a report for:
			<select id="classes">
			<?php
				include("write_functions.php");
				connect("localhost", "root", "", "gradegriddatabase");
				
				//Get the details for this account.
				$account_query = sprintf("SELECT * FROM account WHERE username = '%s'",
				                         mysql_real_escape_string($_SESSION["username"]));
				$account = mysql_fetch_array(mysql_query($account_query));
				
				//Get all classes listed under this guy.
				$class_query = sprintf("SELECT class_id, class_name, course_name FROM class, course WHERE
				                        class.course_id = course.course_id and class.account_id = '%s'",
				                       mysql_real_escape_string($account["account_id"]));
				$cq = mysql_query($class_query);
				//$class = mysql_fetch_array($cq);
				
				//$class_requested = $_GET['course'] . " - " . $_GET['section'];
				
				$i = 0;
				$limit = mysql_num_rows($cq);
				
				while($i < $limit){
					mysql_data_seek($cq, $i);
					$class = mysql_fetch_array($cq);
					$classid = $class["class_id"];
					$classname = $class["class_name"];
					$coursename = $class["course_name"];
					echo "<option value='$classid'>$coursename - $classname</option>\n";
					$i++;
				}
			?>
			</select>
			</p>
			
			<p>Generate what type of report?</p>
			<span class="pMirror">
			<!--<input type="radio" checked="checked" name="reporttype" value="grades" id="grades" />
			<label for="grades">Student standings</label><br />-->
			<input type="radio" name="reporttype" value="pfrecord" checked="checked" id="pfrecord" />
			<label for="pfrecord">Printer-friendly class record</label><br />
			<input type="radio" name="reporttype" value="markedrecord" id="markedrecord" />
			<label for="markedrecord">Class record with student marks</label><br />
			<input type="radio" name="reporttype" value="csv" id="csv" />
			<label for="csv">Comma-separated values (CSV) file</label>
			<p>Generating a CSV from your class record allows you to import your database into a spreadsheet
			without worrying about what software will read it (OpenOffice Calc, Microsoft Excel, etc.). You can
			learn how to import here.</p>
			<input type="button" onclick="generateReport()" value="Generate Report" />
			</div>
		<?php else: ?>
			<?php executeFile("../files/access_error.txt"); ?>
		<?php endif ?>
	</div>
	<div id="copywright"></div>
</body>
</html>